<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Send_lib {
	
	private $CI;
	
	private $out_time = 5;
	private $send_type = "curl"; //curl file
	private $header_arr = array();
	
	
	
	// --------------------------------------------------------------------
	
	public function get($url,$set_params = array(),$header = array())
	{
		if($this->send_type == "curl")
		{
			return $this->curl_post_get($url,$set_params,"GET",$header);
		}
		else
		{
			return $this->file_post_get($url,$set_params,"GET",$header);
		}
		
	}
	
	public function post($url,$set_params = array(),$header = array())
	{
		if($this->send_type == "curl")
		{
			return $this->curl_post_get($url,$set_params,"POST",$header);
		}
		else
		{
			return $this->file_post_get($url,$set_params,"POST",$header);
		}
		
	}
	
	public function data($url,$set_params = array(), $header = array(), $type = "json")
	{
		if($type == "json")
		{
			$set_params = json_encode($set_params);
			$header["Content-Type"] = "Content-Type: application/json";
			$header["Content-Length"] = "Content-Length: " . strlen($set_params);
			
		}
		
		
		if($this->send_type == "curl")
		{
			return $this->curl_post_get($url,$set_params,"POST",$header);
		}
		else
		{
			return $this->file_post_get($url,$set_params,"POST",$header);
		}
	}
	
	/**
	 * @param string $url 请求URL
	 * @param array $set_params 请求参数
	 * @param string $method 请求方法GET/POST/PUT/DELETE
	 * @param array $header HTTP头信息
	 */
	private function curl_post_get($url,$set_params = array(),$method = "GET",$header = array(),$return_type = "json")
	{
		//$header['Host'] = "Host: api.shop.cn";
		
		$header = $header + $this->header_arr;
		
		$opts = array(
			CURLOPT_TIMEOUT => $this->out_time,	//超时时间
			CURLOPT_RETURNTRANSFER => true, //返回的内容作为变量储存
			CURLOPT_SSL_VERIFYPEER => false,	//不验证证书
			CURLOPT_SSL_VERIFYHOST => false,  //不验证证书
			CURLOPT_HTTPHEADER => $header,
			//CURLOPT_AUTOREFERER =>true,
			//CURLOPT_FOLLOWLOCATION => true,
			//CURLOPT_FRESH_CONNECT => true, //TRUE 强制获取一个新的连接，而不是缓存中的连接。
		);
		
		//检测发送方式
		switch (strtoupper($method)){
			
			case 'GET':
				if($set_params)
				{
					$opts[CURLOPT_URL] = $url . (stripos($url, '?') !== false)? "&":"?" . http_build_query($set_params);
				}
				else
				{
					$opts[CURLOPT_URL] = $url;
				}
				
				break;
			case 'POST':
				if (is_array($set_params)){
					$set_params = http_build_query($set_params);
				}
				//
				$opts[CURLOPT_URL] = $url;
				$opts[CURLOPT_POST] = true;
				$opts[CURLOPT_POSTFIELDS] = $set_params;
				
				break;
			default:
				break;
		}
		
		$ch = curl_init();
		curl_setopt_array($ch, $opts);
		$data = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);
		
		if($error){
			$error_data = array("type"=>404,"data"=>null,"message"=>$error);
			
			if($return_type == "json")
			{
				$error_data = json_encode($error_data);
			}
			elseif($return_type == "text")
			{
				$error_data = $error;
			}
			return $error_data;
		}
		return $data;
		
	}
	
	private function file_post_get($url,$set_params = array(),$method = "GET",$header = array(),$return_type = "json")
	{
		$header = $header + $this->header_arr;
		
		switch (strtoupper($method)){
			case "GET":
				if($set_params)
				{
					$url = $url . (stripos($url, '?') !== false)? "&":"?" . http_build_query($set_params);
				}
				$opts = array(
					"http" => array(
						"method" => "GET",
						"header" => "Content-type: application/x-www-form-urlencoded\r\n" ,
							//"Content-Length:" . strlen($set_params)."\r\n" ,
						//"content" => $set_params
					)
				);
				$context = stream_context_create($opts);
				$html = file_get_contents($url, false, $context);
				break;
				
			case "POST":
				if (is_array($set_params)){
					$set_params = http_build_query($set_params);
				}
				$opts = array(
					"http" => array(
						"method" => "POST",
						"header" => "Content-type: application/x-www-form-urlencoded\r\n" . 
							"Content-Length:" . strlen($set_params)."\r\n" ,
						"content" => $set_params
					)
				);
				
				$context = stream_context_create($opts);
				$html = file_get_contents($url, false, $context);
				break;
		}
		
		return $html; 
		
	}
	

	/**
	 * Constructor
	 *
	 * @param	array	$config
	 * @return	void
	 */
	public function __construct($config = array())
	{
		empty($config) OR $this->initialize($config);
		log_message('info', 'send_lib Class Initialized');
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize preferences
	 *
	 * @param	array	$config
	 * @return	void
	 */
	public function initialize($config = array())
	{
		foreach ($config as $key => $val)
		{
			if (isset($this->$key))
			{
				$this->$key = $val;
			}
		}
	}
	
	public function test()
	{
		echo $this->out_time;
	}
	
}

// EOF